package com.suxin.test.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.StandardCharsets;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/1/14 18:02
 */
@ChannelHandler.Sharable
public class EcoServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        ByteBuf byteBuf = (ByteBuf) msg;

        String bufString = byteBuf.toString(StandardCharsets.UTF_8);

        System.out.println("accpect client: [" + bufString + "]");

        ctx.channel().writeAndFlush(Unpooled.copiedBuffer(
                bufString.concat(System.getProperty("line.separator"))
                        .getBytes(StandardCharsets.UTF_8))
        );

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
